﻿@using System.ComponentModel.DataAnnotations
@using Blazor.Localization
@using Blazor.Pages.UpdateSetting.Modals.Whitelist
@using Blazor.Validators
@inherits Blazor.Core.SoraniFeedbackComponentBase<string, Uri?>
@inject IStringLocalizer<UpdateSettingLocalization> Loc
@inject ILogger<WhitelistModal> Logger

<Form @ref="_form" Model="_model" LabelColSpan="8" WrapperColSpan="16" LabelAlign="AntLabelAlignType.Left">
    <FormItem Label="@Loc[UpdateSettingLocalization.JumpUrl]">
        <Input @bind-value="@context.JumpUrl"/>
    </FormItem>
</Form>

@code
{
    private class FormData
    {
        public string JumpUrl { get; set; }
    }

    private FormData _model;
    private Form<FormData> _form;

    protected override void OnInitialized()
    {
        _model = new FormData()
        {
            JumpUrl = Options
        };
        
        base.OnInitialized();
    }

    public override Task OnFeedbackOkAsync(ModalClosingEventArgs args)
    {
        if (!_form.Validate())
        {
            args.Reject();
            return Task.CompletedTask;
        }

        var uri = new Uri(_model.JumpUrl);
        OkCancelRefWithResult.OkAsync(uri);

        return Task.CompletedTask;
    }
}
